package com.example.administrator.college02.helper;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.administrator.college02.pager.ImageCycleViewListener;
import com.example.administrator.college02.R;
import com.example.administrator.college02.model.Banner;
import com.example.administrator.college02.pager.CycleViewPager;
import com.example.administrator.college02.utils.BannerImageViewUtils;
import com.example.administrator.college02.utils.CommonNet;
import com.example.administrator.college02.utils.HttpMethod;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2016/7/12.
 */
public class AnalyzeJson {

    public static void getDataFromSchoolServer(final List<Banner> banners, final Handler handler, final CycleViewPager mCycleViewPager){
        //https://studentapp-kblog.rhcloud.com/api/life/index?signature=ad569e516e5d6d1b10e607956fd7059f
        new Thread(new Runnable() {
            @Override
            public void run() {
               String str =  HttpMethod.getUrlContent
                        (CommonNet.SHOUYE_SCROLL);
//                OkHttpHelper.getRequest(mContext,client,CommonNet.SHOUYE_SCROLL);

                System.out.println("--------"+str);
                try {
                    JSONObject jo = new JSONObject(str);
                    JSONArray ja = jo.getJSONArray("data");
                    for (int i = 0 ; i < ja.length();i++){
                        JSONObject object = ja.getJSONObject(i);
                        String url = object.getString("school_slider_url_ab");
                        Banner banner = new Banner(url);
                        banners.add(banner);
                        System.out.println("------banner="+banners);
                        System.out.println("--------url="+url);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Message msg = new Message();
                msg.what = 0x123;
                handler.sendMessage(msg);
                //设置轮播
                mCycleViewPager.setWheel(true);
                // 设置轮播时间，默认5000ms
                mCycleViewPager.setTime(2000);
////                设置圆点指示图标组居中显示，默认靠右
//                mCycleViewPager.setIndicatorCenter();
            }
        }).start();

    }
    public static void setBanners(Context mContext,List<ImageView> mImageViews,List<Banner> banners,ImageView bannerImageView,CycleViewPager mCycleViewPager){
        mImageViews.clear();
        for (Banner banner : banners) {
            bannerImageView = BannerImageViewUtils.getImageView(mContext);
            Glide.with(mContext).load(banner.getUrl()).centerCrop()
                    .placeholder(R.mipmap.ic_launcher)
                    .crossFade().into(bannerImageView);
            mImageViews.add(bannerImageView);
        }
        //重新添加第一个view
        bannerImageView = BannerImageViewUtils.getImageView(mContext);
        Glide.with(mContext).load(banners.get(banners.size() - 1).getUrl()).centerCrop()
                .placeholder(R.mipmap.ic_launcher)
                .crossFade().into(bannerImageView);
        mImageViews.add(0, bannerImageView);
        //重新添加最后一个view
        bannerImageView = BannerImageViewUtils.getImageView(mContext);
        Glide.with(mContext).load(banners.get(0).getUrl()).centerCrop()
                .placeholder(R.mipmap.ic_launcher)
                .crossFade().into(bannerImageView);
        mImageViews.add(bannerImageView);
        // 设置循环，在调用setData方法前调用
        mCycleViewPager.setCycle(true);
        // 在加载数据前设置是否循环
        mCycleViewPager.setData(mImageViews, banners, new ImageCycleViewListener() {
            @Override
            public void onImageClick(Object info, int position, View imageView) {

            }
        });
    }
    public static void initBanner(Context mContext, List<ImageView> mImageViews, List<Banner> banners, CycleViewPager mCycleViewPager){
        ImageView bannerImageView = BannerImageViewUtils.getImageView(mContext);
        bannerImageView.setImageResource(R.drawable.life_banner);
        mImageViews.add(bannerImageView);
        // 设置循环，在调用setData方法前调用
        mCycleViewPager.setCycle(false);
        // 在加载数据前设置是否循环
        mCycleViewPager.setData(mImageViews, banners, new ImageCycleViewListener() {
            @Override
            public void onImageClick(Object info, int position, View imageView) {

            }
        });
        //设置轮播
        mCycleViewPager.setWheel(true);
        //设置圆点指示图标组居中显示，默认靠右
        mCycleViewPager.setIndicatorCenter();
        mImageViews.clear();
    }
    public static void initWebView(WebView webView , String url){
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl(url);
    }
}
